<?php
namespace Admin\Controller;
use Common\Model\CaozuologModel;
class TradeController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 广告列表
	 */
	public function orderList(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.add_time':$order = 'o.add_time desc';
			 
		}
		$sortprice=I('sortprice');
		if(!empty($sortprice)){
			if(!in_array($sortprice, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sortprice==1?$order = $order.' , o.price':$order = $order.' , o.price desc';
		
		}
		$uid=I('uid');
		$username=I('username');
		$status=I('status');
		$type=I('type');
		$phone=I('phone');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		$admin = $this->user;
		$where = [];
		if(!empty($uid)){
			$where['o.uid'] = $uid;
		}
		if(!empty($username)){
			$where['u.loginName'] = array('like','%'.$username.'%');
		}
		if(!empty($phone)){
			$where['u.userPhone'] = $phone;
		}
		if(!empty($status)){
			$where['o.status'] = $status;
		}
		if(!empty($type)){
			$where['o.type'] = $type;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('wallet_orders')
				->alias('o')
				->field('o.*,u.loginName,u.userPhone')
				->join('__USERS__ as u on u.userid = o.uid')
				->where($where)
				->limit($pages['limit'],$pages['num'])
				->order($order)
				->select();
		$count =M('wallet_orders')
				->alias('o')
				->join('__USERS__ as u on u.userid = o.uid')
				->where($where)
				->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
		
	}
	/**
	 * 交易列表
	 */
	public function tradeList(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'o.over_time':$order = 'o.over_time desc';
	
		}
		$sortprice=I('sortprice');
		if(!empty($sortprice)){
			if(!in_array($sortprice, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sortprice==1?$order = $order.' ,o.price':$order = $order.' ,o.price desc';
		
		}
		$buy_uid=I('buy_uid');
		$sell_uid=I('sell_uid');
		$buy_name=I('buy_name');
		$sell_name=I('sell_name');
		$status=I('status');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		$admin = $this->user;
		$where = [];
		if(!empty($buy_uid)){
			$where['o.buy_uid'] = $buy_uid;
		}
		if(!empty($sell_uid)){
			$where['o.sell_uid'] = $sell_uid;
		}
		if(!empty($buy_name)){
			$where['u.username'] = array('like','%'.$buy_name.'%');
		}
		if(!empty($sell_name)){
			$where['us.username'] = array('like','%'.$sell_name.'%');
		}
		if(!empty($status)){
			if($status==3){
				$status=0;  
			}
			$where['o.trade_status'] = $status;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['o.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$list = M('Trade')
		->alias('o')
		->field('o.*,u.username as buy_name,us.username as sell_name')
		->join('__MEMBER__ as u on u.member_id = o.buy_uid')
		->join('__MEMBER__ as us on us.member_id = o.sell_uid')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		$count =M('Trade')
		->alias('o')
		->join('__MEMBER__ as u on u.member_id = o.buy_uid')
		->join('__MEMBER__ as us on us.member_id = o.sell_uid')
		->where($where)
		->count();
		$return = [
		'count'=>$count,
		'data'=>$list,
		'status'=>1
		];
		$this->ajaxReturn($return);
	
	}
	/**
	 * 查看凭证
	 */
	public function getTradePic(){
		$id=I('id');
		$list=M('wallet_trade_pic')->where('trade_id ='.$id)->find();
		$data['list']=$list;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	/**
	 * 下架广告
	 */
	public function cancelOrders(){
		$id=I('id');
		$order=M('wallet_orders')->where('id ='.$id)->find();
		if(!$order||$order['status']==-1){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($data);
		}
		$r=M('wallet_orders')->where('id ='.$id)->setField('status',-1);
		if($r){
			$msg['status']=1;
			$msg['info']='下架成功';
			$M_LOG = new CaozuologModel();
			$content = '下架广告，ID为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
			$this->ajaxReturn($msg);
		}else{
			$msg['status']=0;
			$msg['info']='服务器繁忙，请稍后再试';
			$this->ajaxReturn($msg);
		}
		
	}
	/**
	 * 结束交易
	 */
	public function cancelTrade(){
		$id=I('id');
		$order=M('wallet_trade')->where('id ='.$id)->find();
		if(!$order||$order['trade_status']!=1){
			$data = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($data);
		}
		$r[]=M('wallet_trade')->where('id ='.$id)->setField('trade_status',-1);
		$r[]=M('wallet')->where('uid ='.$order['sell_uid'])->setDec('fyl_trade_num',$order['num']);
		$r[]=M('wallet')->where('uid ='.$order['sell_uid'])->setInc('fyl_num',$order['num']);
		if(!in_array(false, $r)){
			$msg['status']=1;
			$msg['info']='结束交易成功';
			$M_LOG = new CaozuologModel();
			$content = '结束交易，ID为'.$id;
			$M_LOG->addLog($this->user['username'], $content);
			$this->ajaxReturn($msg);
		}else{
			$msg['status']=0;
			$msg['info']='服务器繁忙，请稍后再试';
			$this->ajaxReturn($msg);
		}
	}
}